package cn.miao.photoprinter.activity;

import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import cn.miao.photoprinter.R;
import cn.miao.photoprinter.adapter.ShoppingCartAdapter;
import cn.miao.photoprinter.database.DatabaseHelper;
import cn.miao.photoprinter.entity.ShoppingCart;
import cn.miao.photoprinter.util.ToastUtil;
import cn.miao.photoprinter.view.SwipeListView;

public class ShoppingCartActivity extends Activity {

	private static final int LOADOK = 10001;
	private static Handler handler;
	SwipeListView shoppingCart;
	Button btSelectAll, btAddcart;
	ShoppingCartAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shoppingcart);
		handler = new MyHandler(this);

		shoppingCart = (SwipeListView) findViewById(R.id.shoppingcart);
		shoppingCart.setRightViewWidth(((int) (getResources()
				.getDisplayMetrics().density * 150)));
		adapter = new ShoppingCartAdapter(this,
				shoppingCart.getRightViewWidth());
		shoppingCart.setAdapter(adapter);
		shoppingCart.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				ShoppingCartAdapter adapter = (ShoppingCartAdapter) parent
						.getAdapter();
				ShoppingCart cart = adapter.getItem(position);
				return false;
			}
		});
		shoppingCart.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ShoppingCartAdapter adapter = (ShoppingCartAdapter) parent
						.getAdapter();
				ShoppingCart cart = adapter.getItem(position);
				// CheckBox checkBox = (CheckBox)
				// view.findViewById(R.id.cbselect);
				// if (checkBox.isChecked()) {
				// cart.setChecked(false);
				// checkBox.setChecked(false);
				// } else {
				// cart.setChecked(true);
				// checkBox.setChecked(true);
				// }
			}
		});

		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					List<ShoppingCart> records = new DatabaseHelper(
							ShoppingCartActivity.this).queryShoppingCart();
					Message msg = new Message();
					msg.what = LOADOK;
					msg.obj = records;
					handler.sendMessage(msg);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}).start();

		btSelectAll = (Button) findViewById(R.id.bt_selectall);
		btAddcart = (Button) findViewById(R.id.bt_addcart);
		btSelectAll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				List<ShoppingCart> list = adapter.getAllCart();
				if ("选择全部".equals(btSelectAll.getText().toString())) {
					btSelectAll.setText("取消全选");
					// select all checkbox
					for (ShoppingCart cart : list) {
						cart.setChecked(true);
					}
				} else {
					btSelectAll.setText("选择全部");
					for (ShoppingCart cart : list) {
						cart.setChecked(false);

					}
				}
				adapter = new ShoppingCartAdapter(ShoppingCartActivity.this,
						list, shoppingCart.getRightViewWidth());
				shoppingCart.setAdapter(adapter);
			}
		});
		btAddcart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				List<ShoppingCart> list = adapter.getCheckedItems();
				// ToastUtil.showTips(ShoppingCartActivity.this,
				// R.drawable.tips_error, "get " + list.size());
				if (list.size() > 0) {
					Intent intent = new Intent(ShoppingCartActivity.this,
							Cart2OrderActivity.class);
					intent.putExtra("data", (Serializable) list);
					startActivity(intent);
				} else {
					ToastUtil.showTips(ShoppingCartActivity.this,
							R.drawable.tips_error, "请选择至少一项");
				}
			}
		});
	}

	@Override
	public void onBackPressed() {
		setResult(Activity.RESULT_OK, new Intent());
		finish();
	}

	static class MyHandler extends Handler {

		WeakReference<ShoppingCartActivity> context;

		public MyHandler(ShoppingCartActivity context) {
			this.context = new WeakReference<ShoppingCartActivity>(context);
		}

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			ShoppingCartActivity activity = context.get();
			switch (msg.what) {
			case LOADOK:
				List<ShoppingCart> records = (List<ShoppingCart>) msg.obj;
				activity.adapter = new ShoppingCartAdapter(activity, records,
						activity.shoppingCart.getRightViewWidth());
				activity.shoppingCart.setAdapter(activity.adapter);
				break;
			}
			super.handleMessage(msg);
		}
	}
}
